package org.happy.tester.hitest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Classname app
 * @Description TODO
 * @Date 2020/6/23 9:25 上午
 * @Created by jc.bai
 */
public class app {

    public static void main(String[] args) {


        /**
         * 数组操作
         */
        Integer[] b = new Integer[]{1, 2, 3};
        Integer[] integers = Arrays.copyOf(b, 2, Integer[].class);
        for (int i = 0; i < integers.length; i++) {
            //System.out.println(integers[i]);
        }
        //System.out.println(b);
        /*int a = 100;
        Arrays.fill(b, 0, 1, a);
        for (int i = 0, len = b.length; i < len; i++) {
            System.out.println(b[i]);
        }*/

        /**
         * 给定一个字符串s，请计算输出含有连续两个s作为子串的最短字符串。注意两个s可能有重叠部分。例如，"ababa"含有两个"aba"
         */
        String str = "abracadabra";
        String temp = "";
        for (int i = 0; i < str.length(); i++) {
            String suffix = str.substring(0, i);
            if (str.endsWith(suffix)) {
                temp = suffix;
            } else {
                break;
            }
        }
        str.contains(str.substring(temp.length()));
        //System.out.println(str);

        /**
         * 获取字符串集合中所有字符串前缀
         */
        List<String> stringList = new ArrayList<>();
        stringList.add("张三aaa");
        stringList.add("张四aaa");
        stringList.add("张三aaa");
        stringList.add("张三aaa");
        stringList.add("张三aaa");
        stringList.add("a三aaa");
        getCommonPrefix(stringList);

    }

    public static void getCommonPrefix(List<String> stringList) {
        if (stringList == null) {
            System.out.println("list is empty ...");
            return;
        }

        if (stringList.size() < 2) {
            System.out.println("list size is : " + stringList.size());
            return;
        }
        String temp = null;
        for (String str : stringList) {
            temp = getCommonPrefixTemp(temp, str);
        }
        System.out.println(temp);
    }

    public static String getCommonPrefixTemp(String temp, String next) {
        if (temp == null) {
            return next;
        }
        for (int i = temp.length(); i > 0; i--) {
            temp = temp.substring(0, i);
            if (next.startsWith(temp)) {
                return temp;
            }
        }
        return "";
    }


}
